package model;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Lees route uit
 * Maakt media items aan van bezienswaardigheden
 * Voegt regel voor regel toe aan de lijst met waypoints
 */
public class RouteParser {

    private List<RouteWaypoint> waypoints;

    public RouteParser() {
        waypoints = new LinkedList<RouteWaypoint>();
    }

    public void parseRoute(String file) {
        Scanner scanner = null;

        try {

            scanner = new Scanner(new BufferedReader(new FileReader(file)));

            while (scanner.hasNextLine()) {


                PointOfInterest pointOfInterest = null;

                String line = scanner.nextLine();

                Scanner lineScanner = new Scanner(line);
                lineScanner.useDelimiter("_");

                int sequenceNumber = lineScanner.nextInt();

                String latitude = lineScanner.next();
                int lat_degrees = Integer.valueOf(latitude.substring(0, 2));
                int lat_minutes = Integer.valueOf(latitude.substring(2, 4));
                double lat_seconds = Double.valueOf(latitude.substring(5, 7) + "." + latitude.substring(7, 9));

                String longitude = lineScanner.next();
                int lon_degrees = Integer.valueOf(longitude.substring(0, 3));
                int lon_minutes = Integer.valueOf(longitude.substring(3, 5));
                double lon_seconds = Double.valueOf(longitude.substring(6, 8) + "." + longitude.substring(8, 10));


                String leeg = null;
                if (lineScanner.hasNext()) {
                    leeg = lineScanner.next();
                }

                String naam = null;
                if (lineScanner.hasNext()) {
                    naam = lineScanner.next();
                    pointOfInterest = new PointOfInterest(naam);
                }

                String points = null;
                if (lineScanner.hasNext()) {
                    points = lineScanner.next();
                }
                
                if (points != null) {
                    
                    String str = points;
                    String[] temp;

                    String delimiter = ",";
                    temp = str.split(delimiter);

                    for (int i = 0; i < temp.length; i++) {
                        
                        if (temp[i].substring(0, 1).equals("P")) {
                            MultiMedia img = new ImageMultiMedia(temp[i].substring(1));
                            pointOfInterest.addMultiMedia(img);
                        }
                        else if (temp[i].substring(0, 1).equals("T")) {
                            MultiMedia txt = new TextMultiMedia(temp[i].substring(1));
                            pointOfInterest.addMultiMedia(txt);
                        }
                        else if (temp[i].substring(0, 1).equals("V")) {
                            MultiMedia vid = new VideoMultiMedia(temp[i].substring(1));
                            pointOfInterest.addMultiMedia(vid);
                        }
                        else if (temp[i].substring(0, 1).equals("A")) {
                            MultiMedia aud = new AudioMultiMedia(temp[i].substring(1));
                            pointOfInterest.addMultiMedia(aud);
                        }
                    }
                }
                
                waypoints.add(new RouteWaypoint(
                        new GpsCoordinate(
                            new GpsLatitude(lat_degrees, lat_minutes, lat_seconds),
                            new GpsLongitude(lon_degrees, lon_minutes, lon_seconds)),
                        pointOfInterest,
                        sequenceNumber));


            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(RouteParser.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    }

    public List getList() {
        return waypoints;
    }
}
